<script>
	import md5 from 'js-md5';
	import {
		Message
	} from 'element-ui';
	const errjso = {
		"errcode": -1,
		"msg": "发送请求异常请注意查看"
	};

	/**
	 * 捕获请求里如果返回异常
	 */
	function checkToken(data) {
		if (data.status == -103) {
			return false
		}

		return true;
	}
	//请求请成功后统一调用方法
	function httpSuccess(that, response, success, error) {

		var data = response.data; //console.log(data);
		// console.log(data);
		// if(!checkToken(data)){//token验证失败
		//   that.$router.push({path:'/login'})
		//   that.localStorage.removeItem("userMap");
		//   that.localStorage.removeItem("token");
		// }
		if (data.status == 106) {

			that.$router.push({
				path: '/login',
				params: {
					wqe: 1
				}
			})
			return
		}
		if (data.status == -102) {
			that.$message.error(data.msg);
			return
		}

		if (data.status == 0) {
			success(data);
		} else {
			that.$message.error(data.msg);
		}
	}

	/**
	 * 反向字符串拼接
	 * @param json
	 * @returns {string}
	 */
	function valueReverse(json) {
		let arrays = "myistest0000";
		for (var key in json) {
			if (key == "signature") {
				continue;
			}
			arrays += "," + key;
		}
		if (arrays == "myistest0000") {
			return;
		}

		let array = arrays.split(",");
		array.sort().reverse();
		var jsonStr = "";
		for (var i in array) {

			if ((array[i] == "myistest0000" || json[array[i]] == "" || json[array[i]] == undefined) && json[array[i]] != 0) {
				continue;
			}
			// jsonStr += json[array[i]];
			if (typeof(json[array[i]]) == "object") {
				jsonStr += JSON.stringify(json[array[i]]);
			} else {
				jsonStr += json[array[i]];
			}
		}
		return jsonStr;
	}
	export default {
		/**
		 * get请求 非加密啊
		 * @param that
		 * @param url
		 * @param json
		 * @param success
		 * @param error
		 */
		get(that, url, json, success, error) {
			that.$axios({
					method: 'get',
					url: url,
					params: json
				}).then(function(response) {
					httpSuccess(that, response, success, error)
				})
				.catch(function(response) { //异常时
					//console.log(response);

				});
		},
		/**
		 * post请求 非加密
		 * @param that
		 * @param url
		 * @param json
		 * @param success
		 * @param error
		 */
		post(that, url, json, success, error) {
			that.$axios({
					method: 'post',
					url: url,
					params: json
				}).then(function(response) {
					httpSuccess(that, response, success, error)

				})
				.catch(function(response) { //异常时
					//console.log(response);

				});
		},

		/**
		 * delete请求 非加密
		 * @param that
		 * @param url
		 * @param json
		 * @param success
		 * @param error
		 */
		delete(that, url, json, success, error) {
			that.$axios({
					method: 'delete',
					url: url,
					params: json
				}).then(function(response) {
					httpSuccess(that, response, success, error)

				})
				.catch(function(response) { //异常时
					//console.log(response);

				});
		},

		/**
		 * put请求 非加密
		 * @param that
		 * @param url
		 * @param json
		 * @param success
		 * @param error
		 */
		put(that, url, json, success, error) {
			that.$axios({
					method: 'put',
					url: url,
					params: json
				}).then(function(response) {
					httpSuccess(that, response, success, error)

				})
				.catch(function(response) { //异常时
					//console.log(response);

				});
		},

		/**
		 * 加密get请求
		 * @param that
		 * @param url
		 * @param json
		 * @param success
		 * @param error
		 */
		encryptGet(that, url, json, success, error) {
			let values = md5(valueReverse(json));
			json.signature = values;
			try {
				that.$axios({
						method: 'get',
						url: url,
						params: json
					}).then(function(response) {
						httpSuccess(that, response, success, error)

					})
					.catch(function(response) { //异常时
						//console.log(response);

					});
			} catch (e) {
				console.log(e)
			}

		},
		/**
		 * 加密post请求
		 * @param that
		 * @param url
		 * @param json
		 * @param success
		 * @param error
		 */
		encryptPost(that, url, json, success, error) {
			let values = md5(valueReverse(json));
			json.signature = values;
			try {
				that.$axios({
					method: 'post',
					url: url,
					params: json
				}).then(function(response) {
					httpSuccess(that, response, success, error)

				})
			} catch (e) {
				console.log(e)
			}
		},

		/**
		 * 加密put请求
		 * @param that
		 * @param url
		 * @param json
		 * @param success
		 * @param error
		 */
		encryptPut(that, url, json, success, error) {
			let values = md5(valueReverse(json));
			json.signature = values;
			try {
				that.$axios({
					method: 'put',
					url: url,
					params: json
				}).then(function(response) {
					httpSuccess(that, response, success, error)
				})
			} catch (e) {
				console.log(e)
			}
		},

		/**
		 * 加密delete请求
		 * @param that
		 * @param url
		 * @param json
		 * @param success
		 * @param error
		 */
		encryptDelete(that, url, json, success, error) {
			let values = md5(valueReverse(json));
			json.signature = values;
			try {
				that.$axios({
					method: 'delete',
					url: url,
					params: json
				}).then(function(response) {
					httpSuccess(that, response, success, error)
				})
			} catch (e) {
				console.log(e)
			}
		},

		upload(that, url, json, success, error) {
			that.$axios.post(
					url,
					json, {
						headers: {
							'Content-Type': 'multipart/form-data'
						}
					},
				).then(function(response) {
					success(response.data);

				})
				.catch(function(response) { //异常时
					// console.log(response);
					error(response.data);

				});
		},

		getUrl(that, url, json, success, error) {
			let values = md5(valueReverse(json));
			json.signature = values;
			try {
				that.$axios({
						method: 'get',
						url: url,
						params: json
					}).then(function(response) {
						httpSuccess(that, response, success, error)

					})
					.catch(function(response) { //异常时
						//console.log(response);

					});
			} catch (e) {
				console.log(e)
			}
		}

	}
</script>
